package com.shxc.smarttrade.old.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.PendOrder;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/10.
 */

public class PositionPendingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    LayoutInflater mInflater;
    Context mContext;
    List<PendOrder.DataBean.PendOrderBean> pendOrderList;
    private int tag = 0;// 标记是否是第一次进入
    private List<double[]> realTimeData;//0:market,1:ask,2:bid
    private PendingItemClickListener listener;

    public PositionPendingAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<PendOrder.DataBean.PendOrderBean> pendOrderList, int tag, List<double[]> realTimeData) {
        this.pendOrderList = pendOrderList;
        this.realTimeData = realTimeData;
        this.tag = tag;
        notifyDataSetChanged();
    }

    public void setOnPendingItemClickListener(PendingItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PositionPendingViewholder(mInflater.inflate(R.layout.adapter_pending_order, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof PositionPendingViewholder) {
            final PendOrder.DataBean.PendOrderBean pendOrder = pendOrderList.get(position);
            int digits = pendOrder.getDigits();
            ((PositionPendingViewholder) holder).tvName.setText(pendOrder.getSymbolCn());
            ((PositionPendingViewholder) holder).tvNameCn.setText("(" + pendOrder.getSymbolEn() + ")");
            if (pendOrder.getCmd() == 2 || pendOrder.getCmd() == 4) {
                ((PositionPendingViewholder) holder).tvBuyRise.setImageResource(R.mipmap.icon_buy);
            } else {
                ((PositionPendingViewholder) holder).tvBuyRise.setImageResource(R.mipmap.icon_sell);
            }

            ((PositionPendingViewholder) holder).tvPendingPrice.setText(BaseUtils.getDigitsData(pendOrder.getOpenPrice(), digits));
            ((PositionPendingViewholder) holder).tvVolume.setText(pendOrder.getVolume() + "手");
            if (tag == 1) {
                double[] real = new double[3];
                if (realTimeData.size() > position) {
                    real = realTimeData.get(position);
                }
                if (pendOrder.getCmd() == 2 || pendOrder.getCmd() == 4)
                    ((PositionPendingViewholder) holder).tvRealTimePrice.setText(BaseUtils.getDigitsData(real[2], digits));
                else
                    ((PositionPendingViewholder) holder).tvRealTimePrice.setText(BaseUtils.getDigitsData(real[1], digits));
            } else {
                ((PositionPendingViewholder) holder).tvRealTimePrice.setText(BaseUtils.getDigitsData(pendOrder.getMarket(), digits));

            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.itemClick(pendOrder, position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return pendOrderList == null ? 0 : pendOrderList.size();
    }

    public class PositionPendingViewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_name_cn)
        TextView tvNameCn;
        @BindView(R.id.tv_buy_rise)
        ImageView tvBuyRise;
        @BindView(R.id.tv_real_time_price)
        TextView tvRealTimePrice;
        @BindView(R.id.tv_pending_price)
        TextView tvPendingPrice;
        @BindView(R.id.tv_volume)
        TextView tvVolume;


        PositionPendingViewholder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

    public interface PendingItemClickListener {
        void itemClick(PendOrder.DataBean.PendOrderBean pendOrderBean, int position);
    }
}
